﻿using System;
using System.Collections;

namespace UniRx.Operators
{
	// Token: 0x020003F1 RID: 1009
	internal class FromMicroCoroutineObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x060014A5 RID: 5285 RVA: 0x0005D22C File Offset: 0x0005B62C
		public FromMicroCoroutineObservable(Func<IObserver<T>, CancellationToken, IEnumerator> coroutine, FrameCountType frameCountType) : base(false)
		{
			this.coroutine = coroutine;
			this.frameCountType = frameCountType;
		}

		// Token: 0x060014A6 RID: 5286 RVA: 0x0005D244 File Offset: 0x0005B644
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			FromMicroCoroutineObservable<T>.FromMicroCoroutine arg = new FromMicroCoroutineObservable<T>.FromMicroCoroutine(observer, cancel);
			BooleanDisposable booleanDisposable = new BooleanDisposable();
			CancellationToken arg2 = new CancellationToken(booleanDisposable);
			switch (this.frameCountType)
			{
			case FrameCountType.Update:
				MainThreadDispatcher.StartUpdateMicroCoroutine(this.coroutine(arg, arg2));
				break;
			case FrameCountType.FixedUpdate:
				MainThreadDispatcher.StartFixedUpdateMicroCoroutine(this.coroutine(arg, arg2));
				break;
			case FrameCountType.EndOfFrame:
				MainThreadDispatcher.StartEndOfFrameMicroCoroutine(this.coroutine(arg, arg2));
				break;
			default:
				throw new ArgumentException("Invalid FrameCountType:" + this.frameCountType);
			}
			return booleanDisposable;
		}

		// Token: 0x04000BF5 RID: 3061
		private readonly Func<IObserver<T>, CancellationToken, IEnumerator> coroutine;

		// Token: 0x04000BF6 RID: 3062
		private readonly FrameCountType frameCountType;

		// Token: 0x020003F2 RID: 1010
		private class FromMicroCoroutine : OperatorObserverBase<T, T>
		{
			// Token: 0x060014A7 RID: 5287 RVA: 0x0005D2E6 File Offset: 0x0005B6E6
			public FromMicroCoroutine(IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
			}

			// Token: 0x060014A8 RID: 5288 RVA: 0x0005D2F0 File Offset: 0x0005B6F0
			public override void OnNext(T value)
			{
				try
				{
					this.observer.OnNext(value);
				}
				catch
				{
					base.Dispose();
					throw;
				}
			}

			// Token: 0x060014A9 RID: 5289 RVA: 0x0005D32C File Offset: 0x0005B72C
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x060014AA RID: 5290 RVA: 0x0005D364 File Offset: 0x0005B764
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}
		}
	}
}
